import wizard
import time
import pooler
from osv import osv, fields

class wizard_update(osv.osv_memory):
    _name = 'wizard.update'

    _columns = {
        'name' : fields.char('Name', size = 64, required = True),
        'birthday' : fields.date('Birthday'),
        'gender' : fields.boolean('Gender'),
        'dtb' : fields.float('DTB', digits=(12,6))
    }

    _defaults = {}

    def update_discount_of_books(self, cr, uid, ids, context={}):
        book_ids = self.get_book_ids(cr, uid, ids)
        discount = self.get_discount(cr, uid, ids)
        try:
            self.update_discount(cr, uid, ids, book_ids, discount)
        except Exception, e:
            raise osv.except_osv(('Error'), str(e))

    def get_book_ids(self, cr, uid, ids, context={}):
        current_object = self.read(cr, uid, ids, ['name'])
        return current_object[0]['name']

    def get_discount(self, cr, uid, ids, context={}):
        current_object = self.read(cr, uid, ids, ['dtb'])
        return current_object[0]['dtb']

    def update_discount(self, cr, uid, ids, book_ids, discount):
        book_book = self.get_table(cr, uid, ids, 'ctsv.student')
        val = {
            'discount' : discount,
        }
        book_book.write(cr, uid, book_ids, val)

    def get_table(self, cr, uid, ids, name_of_table):
        pool = pooler.get_pool(cr.dbname)
        return pool.get(name_of_table)

wizard_update()